package com.houzehui.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alipay.api.AlipayApiException;
import com.github.pagehelper.PageInfo;
import com.houzehui.entity.Fund;
import com.houzehui.entity.Tj;
import com.houzehui.entity.Users;
import com.houzehui.service.FundService;
import com.houzehui.service.UsersService;
import com.houzehui.utils.AlibabapayGateWayUtils;
import com.houzehui.utils.FileUtils;
import com.houzehui.utils.PDFUtils;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class FundController {

    @DubboReference
    FundService fundService;
    @DubboReference
    UsersService usersService;

    @RequestMapping("list")
    public String list(HttpServletRequest request, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "3")int pageSize){
        Users user = (Users) request.getSession().getAttribute("user");
        PageInfo<Fund> pageinfo = fundService.pageinfo(pageNum, pageSize);
        request.setAttribute("info",pageinfo);
        request.setAttribute("user",user);
        return "list";

    }

    @RequestMapping("insert")
    public String insert(Integer id,HttpServletRequest request){
        Fund selectbyid = fundService.selectbyid(id);
        request.setAttribute("fund",selectbyid);
        return "insert";
    }
    @RequestMapping("xq")
    public String xq(Integer id,HttpServletRequest request){
        Fund selectbyid = fundService.selectbyid(id);
        request.setAttribute("fund",selectbyid);
        List<Tj> listtj = usersService.listtj(id);
        System.out.println(listtj);
        request.setAttribute("tj",listtj);

        return "xq";
    }



    @RequestMapping(value = "pay",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String pay(HttpServletRequest request,Integer id,Integer num) throws AlipayApiException {
        Users user = (Users) request.getSession().getAttribute("user");
        Fund selectbyid = fundService.selectbyid(id);

        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        long l = snowflake.nextId();
        String did=""+l;
        Double pnum=num*selectbyid.getPrice();
        usersService.insert(did,user.getId(),id,pnum,num);

        String order = AlibabapayGateWayUtils.createOrder(did, pnum.toString(), "购买基金", "");

        return order;
    }

    @RequestMapping("callBack")
    public String callBack(HttpServletRequest request,String out_trade_no,String total_amount){

        usersService.update(out_trade_no);

        return "redirect:grzx";

    }

    @RequestMapping("expo")
    public void expo(HttpServletResponse response, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "3")int pageSize, @RequestParam(defaultValue = "1")int mode) throws Exception {

        PageInfo<Fund> pageinfo = fundService.pageinfo(pageNum, pageSize);
        System.out.println(pageinfo);
        // 1. 生成文档对象-------------
        Document document = new Document(); // 不能忽略

        String s = UUID.randomUUID().toString()+".pdf";

        PdfWriter pdfWriter = PDFUtils.createDoc(document, "d:/mypdf/"+s);//不能忽略
        List<String> list = new ArrayList<String>();
        list.add("基金列表");

        // 将list 中的内容添加  到PDF文件当中
        //--------------------
        PDFUtils.addContent(document, list, 18);
        // 具体添加表格--------------------------------------
        PDFUtils.addTable(pdfWriter,new String[] {"id","名称","类型","价格","基金经理"},
                new String[] {"mid","name","typename","price","managename"}, pageinfo.getList(),50,600);

        // ---------------------
        document.close();// 不能忽略
        // ---------------------
        pdfWriter.close();// 不能忽略



        //将刚才生成的pdf 添加图片水印   根据实际情况
        PDFUtils.markImage("d:/pic/1.jpg",
                new FileInputStream("d:/mypdf/"+s),
                new FileOutputStream("d:/mypdf/mark_"+s), "版权所有");


        FileUtils.download(response,"d:/mypdf/mark_"+s,"品牌列表",mode);


    }


}
